package com.carkeeper.user.dao;

import com.carkeeper.user.pojo.Employee;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Insert;
import com.carkeeper.user.pojo.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface EmployeeMapper extends Mapper<Employee> {


    //查询所有员工信息
    @Select("select id,name,phone,email,department_id ,account_status from tb_employee")
    public List<Employee> findAllByEmployee();


    //根据id查询
    @Select("select id,name,phone,email,department_id ,account_status from tb_employee where id=#{id}")
    Employee findById(@Param("id") Integer id);

    //根据id查询员工账号状态
    @Select("select id,account_status from tb_employee where id=#{id}")
    int findAccountStatusById(@Param("id") Integer id);
    /**
     * 根据部门名称查询员工列表
     * @param dept
     * @return
     */
    @Select("select name,phone,email,account_status from tb_employee where department_id = (select id from tb_department where name=#{dept})")
    List<Map> findListByDepartmentName(@Param("dept") String dept);


    /**
     * 根据用户id查询用户的角色关系
     * @param employeeId
     * @return
     */
    @Select("select * from tb_employee_role where employee_id = #{employeeId}")
    List findEmployeeAndRole(@Param("employeeId") Integer employeeId);

    /**
     * 根据用户id删除用户的关系
     * @param employeeId
     * @return
     */
    @Delete("delete from tb_employee_role where employee_id = #{employeeId}")
    int deleteEmployeeAndRole(@Param("employeeId") Integer employeeId);

    /**
     * 根据用户id和角色id为用户授权
     * @param employeeId
     * @param roleId
     * @return
     */
    @Insert("insert into tb_employee_role values (#{employeeId}, #{roleId})")
    int addEmployeeAndRole(@Param("employeeId") Integer employeeId, @Param("roleId") Integer roleId);

    /**
     * 根据用户id查询对应的角色
     * @param employeeId
     * @return
     */
    @Select("select * from tb_role where id in (select role_id from tb_employee_role where employee_id = #{employeeId})")
    List<Role> findRoleByEmployeeId(@Param("employeeId") Integer employeeId);

    /**
     * 根据username查询Role实体
     * @param username
     * @return
     */
    @Select("select * from tb_employee where username = #{username}")
    Employee findAdminByUsername(@RequestParam("username") String username);
}
